import http from './http'

export interface Alert {
  id: string
  alertname: string
  severity: string
  status: string
  instance: string
  job: string
  startsAt: string
  endsAt: string
  description: string
  labels: Record<string, string>
  annotations: Record<string, string>
  aiAnalysis?: {
    summary: string
    rootCause: string
    suggestions: string[]
    relatedAlerts: string[]
    confidence: number
    tags: string[]
  }
}

export interface AlertsResponse {
  success: boolean
  data: Alert[]
  pagination: {
    current: number
    pageSize: number
    total: number
  }
}

export interface AlertStatisticsResponse {
  success: boolean
  data: {
    total: number
    firing: number
    resolved: number
    acknowledged: number
    bySeverity: {
      critical: number
      high: number
      medium: number
      low: number
    }
  }
}

/**
 * 获取告警列表
 */
export const getAlerts = (params?: {
  status?: string
  severity?: string
  page?: number
  pageSize?: number
  search?: string
}) => {
  return http.get<any, AlertsResponse>('/alerts', { params })
}

/**
 * 获取告警统计信息
 */
export const getAlertStatistics = () => {
  return http.get<any, AlertStatisticsResponse>('/alerts/statistics')
}

/**
 * 获取单个告警详情
 */
export const getAlert = (id: string) => {
  return http.get<any, { success: boolean; data: Alert }>(`/alerts/${id}`)
}

/**
 * 确认告警
 */
export const acknowledgeAlert = (id: string) => {
  return http.post(`/alerts/${id}/acknowledge`)
}

/**
 * 解决告警
 */
export const resolveAlert = (id: string) => {
  return http.post(`/alerts/${id}/resolve`)
}

/**
 * 静默告警
 */
export const silenceAlert = (id: string, data: { duration: number; comment: string }) => {
  return http.post(`/alerts/${id}/silence`, data)
}

/**
 * AI 分析告警
 */
export const analyzeAlertWithAI = (id: string) => {
  return http.post<any, { success: boolean; data: Alert['aiAnalysis'] }>(`/alerts/${id}/analyze`)
}
